package com.meida.base.provider.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.meida.module.admin.client.entity.BaseRole;
import com.meida.module.admin.client.entity.BaseRoleUser;
import com.meida.module.admin.provider.service.BaseRoleService;
import com.meida.common.mybatis.model.*;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zyf
 */
@Api(tags = "系统角色管理")
@RestController
public class BaseRoleController {
    @Autowired
    private BaseRoleService baseRoleService;

    /**
     * 获取分页角色列表
     *
     * @return
     */
    @ApiOperation(value = "获取分页角色列表", notes = "获取分页角色列表")
    @GetMapping("/role/page")
    public ResultBody<IPage<BaseRole>> pageList(@RequestParam(required = false) Map params) {
        CriteriaQuery<BaseRole> cq = new CriteriaQuery(params, BaseRole.class);
        BaseRole query = cq.getEntity(BaseRole.class);
        cq.lambda()
                .eq(FlymeUtils.isNotEmpty(query.getRoleName()), BaseRole::getRoleName, query.getRoleName())
                .eq(FlymeUtils.isNotEmpty(query.getRoleCode()), BaseRole::getRoleCode, query.getRoleCode());
        return baseRoleService.pageList(params);
    }

    /**
     * 获取所有角色列表
     *
     * @return
     */
    @ApiOperation(value = "获取所有角色列表", notes = "获取所有角色列表")
    @GetMapping("/role/all")
    public ResultBody<List<BaseRole>> getRoleAllList() {
        return ResultBody.ok(baseRoleService.findAllList());
    }

    /**
     * 获取角色详情
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "获取角色详情", notes = "获取角色详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", defaultValue = "", required = true, paramType = "path")
    })
    @GetMapping("/role/{roleId}/info")
    public ResultBody<BaseRole> getRole(@PathVariable(value = "roleId") Long roleId) {
        BaseRole result = baseRoleService.getRole(roleId);
        return ResultBody.ok(result);
    }

    /**
     * 添加角色
     *
     * @param model 角色
     * @return
     */
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @PostMapping("/role/add")
    public ResultBody addRole(SaveModel model) {
        return baseRoleService.add(model);
    }

    /**
     * 编辑角色
     *
     * @param model
     * @return
     */
    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @PostMapping("/role/update")
    public ResultBody updateRole(@ModelAttribute(value = "model") UpdateModel model) {
        return baseRoleService.edit(model);
    }


    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @PostMapping("/role/remove")
    public ResultBody removeRole(DeleteModel model) {
        baseRoleService.removeRole(model);
        return ResultBody.ok();
    }

    /**
     * 角色添加成员
     *
     * @param roleId
     * @param userIds
     * @return
     */
    @ApiOperation(value = "角色添加成员", notes = "角色添加成员")
    @PostMapping("/role/users/add")
    public ResultBody addUserRoles(
            @RequestParam(value = "roleId") Long roleId,
            @RequestParam(value = "userIds", required = false) String userIds
    ) {
        baseRoleService.saveRoleUsers(roleId, StringUtils.isNotBlank(userIds) ? userIds.split(",") : new String[]{});
        return ResultBody.ok();
    }

    /**
     * 查询角色成员
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "查询角色成员", notes = "查询角色成员")
    @GetMapping("/role/users")
    public ResultBody<List<BaseRoleUser>> getRoleUsers(
            @RequestParam(value = "roleId") Long roleId
    ) {
        return ResultBody.ok(baseRoleService.findRoleUsers(roleId));
    }

}
